<?php
/**
 * 住院患者模型
 * @authors xiadx (xiadx@zpdchina.com)
 * @date    2017-03-05 21:16:24
 * @version $Id$
 */

class ZhuyuanBasicInfoModel extends BaseModel {

	/**
     * 返回给终端的可执行sql语句的字段名称数组
     * @var [type]
     */
    public $_field_arr = ['zhuyuan_id','patient_id','xingming','xingbie','nianling','zhuyuan_bingqu','ruyuan_riqi_time','bingchuang_hao', 'hulijibie','zhenduan','zhuangtai','his_display_zhuyuanhao','zhuzhenyishi','zerenhushi','ruyuan_qingkuang','yiliaofukuanfangshi','shengri','bingchuang_fenzu','chushengdi','gongzuo_dianhua','juzhu_dianhua','lianxiren_dianhua','blood_type',
    'guominshi','zhusu','shenfenzheng_hao','chuyuan_riqi_time','modify_time','huanzhe_leixing',
    ];

    public function ls($user_department,$modify_time = '')
    {
    	$field_str = 't.zhuangtai,t.zhuyuan_id,t.patient_id, t.zhenduan, t.hulijibie,t.zhuyuan_bingqu,t.bingchuang_fenzu, t.zhuzhenyishi, t.zerenhushi, t.ruyuan_riqi_time, t.ruyuan_qingkuang, t.yiliaofukuanfangshi, a.xingming, a.xingbie, a.nianling, a.shengri, t.his_display_zhuyuanhao, t.bingchuang_hao,a.chushengdi, b.gongzuo_dianhua, b.juzhu_dianhua, b.lianxiren_dianhua, t.blood_type, t.guominshi, t.zhusu,a.shenfenzheng_hao,t.chuyuan_riqi_time,t.modify_time,t.huanzhe_leixing';

    	$sql_where = " t.yiyuan_id = " . $this->yiyuan_id;
		if($modify_time){
			$sql_where .= " and t.modify_time>='$modify_time'";
		}
		if($user_department){
			$sql_where .= " and t.zhuyuan_bingqu = '".$user_department."'";
		}

        // TODO 患者保留时间
		$huanzhe_chuyuan_baoliu_shijian = [
            "value"=>"2",
            "unit"=>"DAY",
        ];
		if(isset($huanzhe_chuyuan_baoliu_shijian))
		{

			if(empty($sql_where)){
				$sql_where .= " and t.zhuangtai = '住院中' or (t.zhuangtai = '已出院' and ".Utility::getIntervalSqlString("a.chuyuan_riqi_time",$huanzhe_chuyuan_baoliu_shijian).")";
			}else{
				$sql_where .= " and (t.zhuangtai = '住院中' or (t.zhuangtai = '已出院' and ".Utility::getIntervalSqlString("t.chuyuan_riqi_time",$huanzhe_chuyuan_baoliu_shijian)."))";
			}
		}else{
			if(empty($sql_where)){
				$sql_where .= " and t.zhuangtai = '住院中'";
			}else{
				$sql_where .= " and t.zhuangtai = '住院中'";
			}
		}
		$sql = "SELECT $field_str FROM zhuyuan_basic_info t LEFT JOIN patient_basic_info a on t.patient_id=a.patient_id LEFT JOIN patient_contact_info b on a.patient_id=b.patient_id  where ".$sql_where;
		$query = DB::query(Database::SELECT, $sql)->execute()->as_array();
		return $query;
    }

    /**
     * 获取住院ID
     * @param  [type] $user_department [description]
     * @param  [type] $modify_time     [description]
     * @return [type]                  [description]
     */
    public function getZhyuanIdList($user_department, $modify_time = '')
    {
    	$query = DB::select('zhuyuan_id')
    				->from('zhuyuan_basic_info');
		if ($modify_time) {
			$query->and_where('modify_time', '>=', $modify_time);
		}
		if ($user_department) {
			$query->and_where('zhuyuan_bingqu', '=', $user_department);
		}

		/*患者出院保留时间*/
        $yiyuan_id = $this->yiyuan_id;
		$huanzhe_chuyuan_baoliu_shijian = Yaconf::get("config$yiyuan_id.huanzhe_chuyuan_baoliu_shijian");

        if ($huanzhe_chuyuan_baoliu_shijian) {
            $intval = Utility::getIntervalSqlArray('chuyuan_riqi_time',$huanzhe_chuyuan_baoliu_shijian);
            $query->and_where_open()
                    ->where('zhuangtai', '=', '住院中')
                    ->or_where_open()
                        ->where('zhuangtai', '=', '已出院')
                        ->and_where('chuyuan_riqi_time', '>=', DB::expr($intval))
                    ->or_where_close()
                ->and_where_close();
        } else
        	$query->and_where('zhuangtai', '=', '住院中');
        return $query->execute()->as_array();
    }
}
